﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class UIVRK : ViewBase
    {
        public UIVRK()
        {

        }

        public void ResetSeq(object sender, RoutedEventArgs e)
        {
            if (!TMV.IsITX)
            {
                return;
            }
            VSDDAT.SD22.RESETSEQ("0022", "VR", "VRRENR", "1");
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            TMV.DeleteVRKOPF(dwData, 2);
            dwData = TMV.VRP.TBMain.NewRow();
            dwData["VRPMANDANT"] = app.Mandant;
            dwData["VRPAUFKRS"] = KRS;
            TMV.VRP.DeleteVRPOS(dwData, 3);
            dwData = TMV.VZB.TBMain.NewRow();
            dwData["VKZMANDANT"] = app.Mandant;
            dwData["VKZAUFKRS"] = "15";
            dwData["VKZSA"] = "VR";
            TMV.VZB.DeleteVKZUAB(dwData, 0);

        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return; 
            base.Window_Loaded(sender, e);
            AUFTYP = "K";
        }
        public void KD02Command(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.KD02(RUNID, KUNDNR);
        }
        public void BackToNormalCommand(object sender, RoutedEventArgs e)
        {
            KDBLOCK = false;
        }


        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 8)
                return;
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKRENR"] = ORDERNUM.Substring(0, 8);
            SResult = ((VVRKOPF)MV).QueryVRKOPF(dwData, 1);
            MergeData(SResult, true, true);
        }

        public virtual void OnVKKChg(string value)
        {
            TBMain.Clear();
            if (value.Length < 9)
                return;
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKVERBNR"] = value.Substring(1, 8);
            dwData["VRKZESSION"] = value.Substring(0, 1);
            SResult = ((VVRKOPF)MV).QueryVRKOPF(dwData, 6);
            MergeData(SResult, true, true);
        }

        public virtual void OnAUFTYPChg(string value)
        {

        }
        VKUNDADR kad;

        public virtual void OnADSLChg(string _adsl)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (_adsl.Length < 16)
            {
                dwMaster["VRKADRSL"] = "".PadRight(8);
                return;
            }
            dwMaster["VRKADRSL"] = _adsl.Substring(8, 8);

            if (kad == null) kad = new VKUNDADR();
            dwData = kad.TBMain.NewRow();
            dwData["KDAKUNDNR"] = _adsl.Substring(0, 8);
            dwData["KDAADRSL"] = _adsl.Substring(8, 8);
            SResult = kad.QueryKUNDADR(dwData, 4);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwMaster["VRKNAME1"] = row["KDSNAME1"];
                dwMaster["VRKNAME2"] = row["KDSNAME2"];
                dwMaster["VRKSTRASSE1"] = row["KDSSTRASSE1"];
                dwMaster["VRKSTRASSE2"] = row["KDSSTRASSE2"];
                dwMaster["VRKLKZ"] = row["KDSLKZ"];
                dwMaster["VRKPLZPF"] = row["KDSPLZPF"];
                dwMaster["VRKPOSTF"] = row["KDSPOSTF"];
                dwMaster["VRKPLZ"] = row["KDSPLZ"];
                dwMaster["VRKORT"] = row["KDSORT"];
                dwMaster["VRKBZ"] = row["KDSBZ"];
                dwMaster["VRKBZBEZ"] = row["KDSBZBEZ"];
                dwMaster["VRKCOUNTY"] = row["KDSCOUNTY"];
                dwMaster["VRKCITY"] = row["KDSCITY"];
            }
        }

        public void OnKDChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (value.Length < 16)
            {
                dwMaster["VRKKUNDNR"] = "".PadRight(8);
                VRAD = "";
                return;
            }
            dwMaster["VRKKUNDNR"] = value;
            VRAD = value + "VR".PadRight(8);
            MV.OnPropertyChanged("VRKKUNDNR");
        }

        public void OnZAHLBEDChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["VRKZAHLBED"] = value.PadRight(3);
            MV.OnPropertyChanged("VRKZAHLBED");
        }

        public override void SetUpUserRight()
        {
            base.SetUpUserRight();
            Type mom = this.GetType();
            if (mom == null)
                return;
            string filter = "USERID='" + app.User + "' AND PROG='" + mom.Name.PadRight(80) + "'";
            DataRow[] rows = TBUR.Select(filter);
            foreach (DataRow row in rows)
            {
                string contol = row["CONTROLS"].ToString();//属性 
                string PROCESS = row["PROCESS"].ToString();//控制值
                string txt = row["P0"].ToString();//文本值 
                bool blControl = true;
                if (PROCESS.Trim() == "N")
                {
                    blControl = false; ;
                }
                if (PROCESS.Trim() == "Y")
                {
                    blControl = true; ;
                }
                switch (contol.Trim())
                {
                    case "CHGZA":
                        IsChgZA = blControl;
                        break;
                }
            }
        }
    }
    public partial class UIVRK : ViewBase
    {
        public VVRKOPF TMV
        {
            get
            {
                return (VVRKOPF)MV;
            }
        }
        #region ==IsCanPay===========
        public static readonly DependencyProperty IsCanPayProperty = DependencyProperty.Register("IsCanPay",
            typeof(bool), typeof(UIVRK),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsCanPayChanged));
        public bool IsCanPay
        {
            get
            {
                return (bool)GetValue(IsCanPayProperty);
            }
            set
            {
                SetValue(IsCanPayProperty, value);
            }
        }
        private static void OnIsCanPayChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.IsCanPay = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsChgZA===========
        public static readonly DependencyProperty IsChgZAProperty = DependencyProperty.Register("IsChgZA",
            typeof(bool), typeof(UIVRK),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChgZAChanged));
        public bool IsChgZA
        {
            get
            {
                return (bool)GetValue(IsChgZAProperty);
            }
            set
            {
                SetValue(IsChgZAProperty, value);
            }
        }
        private static void OnIsChgZAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.IsChgZA = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsPaid===========
        public static readonly DependencyProperty IsPaidProperty = DependencyProperty.Register("IsPaid",
            typeof(bool), typeof(UIVRK),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsPaidChanged));
        public bool IsPaid
        {
            get
            {
                return (bool)GetValue(IsPaidProperty);
            }
            set
            {
                SetValue(IsPaidProperty, value);
                OnPaidChg();
            }
        }
        private static void OnIsPaidChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.IsPaid = (bool)e.NewValue;
            }
        }
        public virtual void OnPaidChg()
        {
            if(IsPaid)
            {
                BONKZ = " ";
                
            }
            else
            {
                BONKZ = "N";
            }
        }
        #endregion
        #region ==BONKZ===========
        public static readonly DependencyProperty BONKZProperty = DependencyProperty.Register("BONKZ",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBONKZChanged));
        public string BONKZ
        {
            get
            {
                return (string)GetValue(BONKZProperty);
            }
            set
            {
                SetValue(BONKZProperty, value);
            }
        }
        private static void OnBONKZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.BONKZ = (string)e.NewValue;
            }
        }
        #endregion
        #region ==AUFART===========
        public static readonly DependencyProperty AUFARTProperty = DependencyProperty.Register("AUFART",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFARTChanged));
        public string AUFART
        {
            get
            {
                return (string)GetValue(AUFARTProperty);
            }
            set
            {
                SetValue(AUFARTProperty, value);
            }
        }
        private static void OnAUFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.AUFART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VRAD===========
        public static readonly DependencyProperty VRADProperty = DependencyProperty.Register("VRAD",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVRADChanged));
        public string VRAD
        {
            get
            {
                return (string)GetValue(VRADProperty);
            }
            set
            {
                SetValue(VRADProperty, value);
            }
        }
        private static void OnVRADChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.VRAD = (string)e.NewValue;
            }
        }
        #endregion

        #region ==LIEFNR===========
        public static readonly DependencyProperty LIEFNRProperty = DependencyProperty.Register("LIEFNR",
        typeof(string), typeof(UIVRK),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnLIEFNRChanged));
        public string LIEFNR
        {
            get
            {
                return (string)GetValue(LIEFNRProperty);
            }
            set
            {
                SetValue(LIEFNRProperty, value);
            }
        }
        private static void OnLIEFNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.LIEFNR = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KUNDNR===========
        public static readonly DependencyProperty KUNDNRProperty = DependencyProperty.Register("KUNDNR",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKUNDNRChanged));
        public string KUNDNR
        {
            get
            {
                return (string)GetValue(KUNDNRProperty);
            }
            set
            {
                SetValue(KUNDNRProperty, value);
                OnKDChg(value);
            }
        }
        private static void OnKUNDNRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.KUNDNR = (string)e.NewValue;
            }
        }
        #endregion

        #region ==ADRSL===========
        public static readonly DependencyProperty ADRSLProperty = DependencyProperty.Register("ADRSL",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnADRSLChanged));
        public string ADRSL
        {
            get
            {
                return (string)GetValue(ADRSLProperty);
            }
            set
            {
                SetValue(ADRSLProperty, value);
                OnADSLChg(value);
            }
        }
        private static void OnADRSLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.ADRSL = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VKP===========
        public static readonly DependencyProperty VKPProperty = DependencyProperty.Register("VKP",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKPChanged));
        public string VKP
        {
            get
            {
                return (string)GetValue(VKPProperty);
            }
            set
            {
                SetValue(VKPProperty, value);
                ORDERNUM = VKP;
            }
        }
        private static void OnVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.VKP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==SQTY===========
        public static readonly DependencyProperty SQTYProperty = DependencyProperty.Register("SQTY",
            typeof(double), typeof(UIVRK),
            new FrameworkPropertyMetadata(-1.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSQTYChanged));
        public double SQTY
        {
            get
            {
                return (double)GetValue(SQTYProperty);
            }
            set
            {
                SetValue(SQTYProperty, value);
            }
        }
        private static void OnSQTYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.SQTY = (double)e.NewValue;
            }
        }
        #endregion

        #region ==ZAHLBED===========
        public static readonly DependencyProperty ZAHLBEDProperty = DependencyProperty.Register("ZAHLBED",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnZAHLBEDChanged));
        public string ZAHLBED
        {
            get
            {
                return (string)GetValue(ZAHLBEDProperty);
            }
            set
            {
                SetValue(ZAHLBEDProperty, value);
                OnZAHLBEDChg(value);
            }
        }
        private static void OnZAHLBEDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.ZAHLBED = (string)e.NewValue;
            }
        }
        #endregion

        #region ==VVKP===========
        public static readonly DependencyProperty VVKPProperty = DependencyProperty.Register("VVKP",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVVKPChanged));
        public string VVKP
        {
            get
            {
                return (string)GetValue(VVKPProperty);
            }
            set
            {
                SetValue(VVKPProperty, value);
            }
        }
        private static void OnVVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.VVKP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PAYPAL===========
        public static readonly DependencyProperty PAYPALProperty = DependencyProperty.Register("PAYPAL",
            typeof(int), typeof(UIVRK),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPAYPALChanged));
        public int PAYPAL
        {
            get
            {
                return (int)GetValue(PAYPALProperty);
            }
            set
            {
                SetValue(PAYPALProperty, value);
            }
        }
        private static void OnPAYPALChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.PAYPAL = (int)e.NewValue;
            }
        }
        #endregion

        #region ==VRP===========
        public static readonly DependencyProperty VRPProperty = DependencyProperty.Register("VRP",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVRPChanged));
        public string VRP
        {
            get
            {
                return (string)GetValue(VRPProperty);
            }
            set
            {
                SetValue(VRPProperty, value);
            }
        }
        private static void OnVRPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.VRP = (string)e.NewValue;
            }
        }
        #endregion


        #region ==VKK===========
        public static readonly DependencyProperty VKKProperty = DependencyProperty.Register("VKK",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVKKChanged));
        public string VKK
        {
            get
            {
                return (string)GetValue(VKKProperty);
            }
            set
            {
                SetValue(VKKProperty, value);
                OnVKKChg(value);
            }
        }
        private static void OnVKKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.VKK = (string)e.NewValue;
            }
        }
        #endregion

        #region ==AUFTYP===========
        public static readonly DependencyProperty AUFTYPProperty = DependencyProperty.Register("AUFTYP",
            typeof(string), typeof(UIVRK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUFTYPChanged));
        public string AUFTYP
        {
            get
            {
                return (string)GetValue(AUFTYPProperty);
            }
            set
            {
                SetValue(AUFTYPProperty, value);
                OnAUFTYPChg(value);
            }
        }
        private static void OnAUFTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.AUFTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KDBLOCK===========
        public static readonly DependencyProperty KDBLOCKProperty = DependencyProperty.Register("KDBLOCK",
            typeof(bool), typeof(UIVRK),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKDBLOCKChanged));
        public bool KDBLOCK
        {
            get
            {
                return (bool)GetValue(KDBLOCKProperty);
            }
            set
            {
                SetValue(KDBLOCKProperty, value);
                OnKDBLOCKChg(value);

            }
        }
        private static void OnKDBLOCKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIVRK;
            if (src != null)
            {
                src.KDBLOCK = (bool)e.NewValue;
            }
        }
        public void OnKDBLOCKChg(bool value)
        {


        }
        #endregion
    }
}